package tq.com.tqteacher.mine.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.json.JSONArray;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqteacher.mine.adapter.TQClassOverInfoAdapter;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.net.TQNetInterface;

public class TQClassOverInfoFragment extends BaseRecyclerFragment {
    private TQClassOverInfoAdapter mClassOverInfoAdapter;
    @Override
    protected void initRecycleView(RecyclerView pRecyclerView) {
        super.initRecycleView(pRecyclerView);

        if(mClassOverInfoAdapter == null){
            mClassOverInfoAdapter = new TQClassOverInfoAdapter(getContext());
        }
        pRecyclerView.setAdapter(mClassOverInfoAdapter);
        pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

        getData();
    }

    private void getData(){
        String clsId = String.valueOf(TQLogin.getCurrent().getUserInfo().getClzid());
        TQNetInterface.getClsState(clsId, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
                    AlertDialogHelper.show(TQClassOverInfoFragment.this.getContext(), pNetResult.getErrMsg());
                    return;
                }

                if(pNetResult.getData() == null) return;

                JSONObject lObject = pNetResult.getData().optJSONObject("class");
                if(lObject == null) return;

                JSONObject lUser = lObject.optJSONObject("user");
                mClassOverInfoAdapter.updateOverInfo(lUser);

                JSONArray lArray = lObject.optJSONArray("material");
                mClassOverInfoAdapter.updateSubject(lArray);

            }
        });
    }


}
